//
// DepComponent
//
// Copyright (C) jextra.net.
//
//  This file is part of the net.jextra.dep Library.
//
//  The net.jextra.dep Library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  The net.jextra.dep Library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with the net.jextra.dep Library; if not, write to the Free
//  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//  02111-1307 USA.
//

package net.jextra.dep;

import java.io.*;
import java.util.*;
import net.jextra.dep.repo.*;

/**
 * <p>
 * Represents a single component in the component tree.
 * </p>
 */
public class DepComponent
{
    // ============================================================
    // Fields
    // ============================================================

    private DepRepo repo;

    private DepDependency seedDependency;
    private String name;
    private String versionNumber;

    private LinkedHashSet<DepDependency> dependencies;
    private HashMap<String,DepDependency> templateDependencies;

    private LinkedHashSet<DepComponent> children;
    private ArrayList<DepComponentFile> files;

    // ============================================================
    // Constructors
    // ============================================================

    public DepComponent()
    {
        init();
    }

    // ============================================================
    // Methods
    // ============================================================

    // ----------
    // public
    // ----------

    public DepDependency getSeedDependency()
    {
        return seedDependency;
    }

    public void setSeedDependency( DepDependency dep )
    {
        seedDependency = dep;
    }

    /**
     * The Repository that the component was found in.
     */
    public DepRepo getRepo()
    {
        return repo;
    }

    public void setRepo( DepRepo repo )
    {
        this.repo = repo;
    }

    public void setName( String name )
    {
        this.name = name;
    }

    /**
     * Unique name of the component. This includes the path in the repo but does not include the version (e.g.
     * net.jextra.crack).
     */
    public String getName()
    {
        return name;
    }

    public String getVersionNumber()
    {
        return versionNumber;
    }

    public void setVersionNumber( String versionNumber )
    {
        this.versionNumber = versionNumber;
    }

    public void addDependency( DepDependency dependency )
    {
        dependencies.add( dependency );
    }

    public boolean removeDependency( DepDependency dependency )
    {
        return dependencies.remove( dependency );
    }

    public void removeAllDependencies()
    {
        dependencies.clear();
    }

    public Collection<DepDependency> getDependencies()
    {
        return dependencies;
    }

    public Collection<DepDependency> getMissingDependencies()
    {
        ArrayList<DepDependency> list = new ArrayList<DepDependency>();

        for ( DepDependency dep : dependencies )
        {
            if ( dep.isMissing() )
            {
                list.add( dep );
            }
        }

        return list;
    }

    public void addTemplateDependency( DepDependency dependency )
    {
        templateDependencies.put( dependency.getName().toLowerCase(), dependency );
    }

    public Collection<DepDependency> getTemplateDependencies()
    {
        return templateDependencies.values();
    }

    public DepDependency getTemplateDependency( String name )
    {
        return templateDependencies.get( name.toLowerCase() );
    }

    public void addChild( DepComponent component )
    {
        children.add( component );
    }

    public void removeChild( DepComponent dependency )
    {
        children.remove( dependency );
    }

    public void removeAllChildren()
    {
        children.clear();
    }

    public Collection<DepComponent> getChildren()
    {
        return children;
    }

    public void addFile( DepComponentFile file )
    {
        files.add( file );
    }

    public List<DepComponentFile> getFiles()
    {
        return files;
    }

    public InputStream createInputStream( String filename )
        throws Exception
    {
        if ( repo == null )
        {
            return null;
        }

        return repo.getFileEntryInputStream( name, versionNumber, filename );
    }

    public String getDisplayName()
        throws Exception
    {
        if ( repo == null )
        {
            return null;
        }

        return repo.getComponentDisplayName( name, versionNumber );
    }

    @Override
    public String toString()
    {
        return String.format( "{name=%s,version=%s}", getName(), getVersionNumber() );
    }

    @Override
    public boolean equals( Object obj )
    {
        if ( obj == null || !( obj instanceof DepComponent ) )
        {
            return false;
        }

        DepComponent other = (DepComponent) obj;

        return other.toString().equalsIgnoreCase( toString() );
    }

    @Override
    public int hashCode()
    {
        return name == null ? 0 : name.hashCode();
    }

    // ----------
    // private
    // ----------

    private void init()
    {
        files = new ArrayList<DepComponentFile>();
        dependencies = new LinkedHashSet<DepDependency>();
        templateDependencies = new HashMap<String,DepDependency>();
        children = new LinkedHashSet<DepComponent>();
    }
}
